<?php
    //  收货地址后台数据处理页面
    require_once "weixin_info.php";//å¾®ä¿¡æŽˆæƒèŽ·å–ç”¨æˆ·å¤´åƒç­‰ä¿¡æ?
    require_once "config.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $identity = $_POST['identity'];
        if($identity == 'run'){
            //添加二级管理员
            $mold = empty($_POST['mold']) ? '' : $_POST['mold'];
            $id = empty($_POST['id']) ? '' : $_POST['id'];
            $uid = empty($_POST['uid']) ? '' : $_POST['uid'];
            if ($mold == 'delete') {
                //地址删除
                if(!empty($id)){
                    $address_sql = $mysql->query("select * from dyspw_member where id=".$id);
                    $address_info = $address_sql->fetch_all(MYSQLI_ASSOC);
                    if($address_info){
                        $address_del = $mysql->query("UPDATE dyspw_member SET shop_son_id = '' where id = {$id}");
                        if($address_del){
                            $result['status'] = 'y';
                            $result['info'] = '二级管理员删除成功，即将刷新';
                            exit(json_encode($result));
                        }else{
                            $result['status'] = 'n';
                            $result['info'] = '二级管理员删除失败';
                            exit(json_encode($result));
                        }
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '未查询到该用户信息';
                        exit(json_encode($result));
                    }
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '未获取到该收货地址id';
                    exit(json_encode($result));
                }

            }else{
                $username = $_POST['username'];
                $uid = $_POST['uid'];
                $sql = $mysql->query("select * from dyspw_member where mobile=".$username);
                $mem_info = $sql->fetch_all(MYSQLI_ASSOC);
                $shop_sql = $mysql->query("select * from dyspw_member where id=".$uid);
                $shop_info = $shop_sql->fetch_all(MYSQLI_ASSOC);
                if(empty($mem_info) || $mem_info[0]['is_vip'] != 3){
                    $result['status'] = 'n';
                    $result['info'] = '未查询到商铺信息或还用户信息身份非商户';
                    exit(json_encode($result));
                }
                if($shop_info[0]['is_vip'] != 0){
                    $result['status'] = 'n';
                    $result['info'] = '当前用户角色非游客（二级管理员只限游客）';
                    exit(json_encode($result));
                }
                if(!empty($shop_info[0]['shop_son_id'])){
                    $result['status'] = 'n';
                    $result['info'] = '当前用户已绑定管理员店铺';
                    exit(json_encode($result));
                }else{
                    $add_run = $mysql->query("UPDATE dyspw_member SET shop_son_id = {$mem_info[0]['id']} where id = {$uid}");
                    if($add_run){
                        $result['status'] = 'y';
                        $result['identity'] = 'run';
                        $result['info'] = '编辑成功,即将跳转';
                        exit(json_encode($result));
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '添加失败，请重新提交';
                        exit(json_encode($result));
                    }
                }
            }
        }elseif ($identity == 'guide') {
            //添加导购员
            $mold = empty($_POST['mold']) ? '' : $_POST['mold'];
            $id = empty($_POST['id']) ? '' : $_POST['id'];
            $uid = empty($_POST['uid']) ? '' : $_POST['uid'];
            if ($mold == 'delete') {
                $guide_sql = $mysql->query("select * from dyspw_shop_guide where id=".$id);
                $guide = $guide_sql->fetch_all(MYSQLI_ASSOC);
                if($guide){
                    $address_del = $mysql->query("DELETE FROM  dyspw_shop_guide  where id = {$id} and shop_id = {$uid}");
                    if($address_del){
                        $result['status'] = 'y';
                        $result['info'] = '店铺导购员删除成功，即将刷新';
                        exit(json_encode($result));
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '店铺导购员删除失败';
                        exit(json_encode($result));
                    }
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '未查询到该导购员信息';
                    exit(json_encode($result));
                }
            }else{
                $username = $_POST['username'];
                $uid = $_POST['uid'];
                $sql = $mysql->query("select * from dyspw_member where mobile=".$username);
                $mem_info = $sql->fetch_all(MYSQLI_ASSOC);
                $shop_sql = $mysql->query("select * from dyspw_member where id=".$uid);
                $shop_info = $shop_sql->fetch_all(MYSQLI_ASSOC);
                $guide_sql = $mysql->query("select * from dyspw_shop_guide where guide_id=".$mem_info[0]['id']);
                $guide = $guide_sql->fetch_all(MYSQLI_ASSOC);
                if($mem_info[0]['is_vip'] != 0){
                    $result['status'] = 'n';
                    $result['info'] = '导购员身份必须为游客';
                    exit(json_encode($result));
                }
                if(empty($mem_info)){
                    $result['status'] = 'n';
                    $result['info'] = '根据手机号未查询到该用户，或用户未填写手机号';
                    exit(json_encode($result));
                }else{
                    if(!empty($guide) && $guide[0]['shop_id'] == $shop_info[0]['id']){
                        $result['status'] = 'n';
                        $result['info'] = '该用户已绑定当前商户导购员';
                        exit(json_encode($result));
                    }
                    if(!empty($guide) && $guide[0]['shop_id'] != $shop_info[0]['id']){
                        $result['status'] = 'n';
                        $result['info'] = '该用户已绑定其他商户导购员';
                        exit(json_encode($result));
                    }else{
                        $time = time();
                        $add_run = $mysql->query("INSERT INTO dyspw_shop_guide (shop_id,guide_id,guide_name,guide_avatar,`time`) values ({$shop_info[0]['id']}, {$mem_info[0]['id']}, '{$mem_info[0]['username']}', '{$mem_info[0]['avatar']}', '{$time}')");
                        if($add_run){
                            $result['status'] = 'y';
                            $result['identity'] = 'guide';
                            $result['info'] = '编辑成功,即将跳转';
                            exit(json_encode($result));
                        }else{
                            $result['status'] = 'n';
                            $result['info'] = '添加失败，请重新提交';
                            exit(json_encode($result));
                        }
                    }
                }
            }

        }elseif ($identity == 'store') {
            $mold = empty($_POST['mold']) ? '' : $_POST['mold'];
            $id = empty($_POST['id']) ? '' : $_POST['id'];//分店id
            $uid = empty($_POST['uid']) ? '' : $_POST['uid'];//当前ID
            if($mold == 'delete'){
                $sql = $mysql->query("select * from dyspw_member where id = ".$id);
                $mem_info = $sql->fetch_all(MYSQLI_ASSOC);
                if(empty($mem_info)){
                    $result['status'] = 'n';
                    $result['info'] = '未查询到该分店信息';
                    exit(json_encode($result));
                }
                if(!empty($mem_info[0]['branch_store_id']) && $mem_info[0]['branch_store_id'] == $uid){
                    $store_del = $mysql->query("UPDATE dyspw_member SET branch_store_id = 0 where id = {$id}");
                    if($store_del){
                        $result['status'] = 'y';
                        $result['info'] = '分店商户移除成功';
                        exit(json_encode($result));
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '分店商户移除失败，请重新提交';
                        exit(json_encode($result));
                    }
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '该分店顶级商户非当前商户或顶级商户为空';
                    exit(json_encode($result));
                }
            }else{
                $username = $_POST['username'];
                $uid = $_POST['uid'];
                $sql = $mysql->query("select * from dyspw_member where mobile=".$username);
                $mem_info = $sql->fetch_all(MYSQLI_ASSOC);//总店信息
                $top_mem_sql = $mysql->query("select * from dyspw_member where id=".$uid);
                $top_mem = $top_mem_sql->fetch_all(MYSQLI_ASSOC);
                if(!empty($top_mem[0]['branch_store_id'])){
                    $result['status'] = 'n';
                    $result['info'] = '当前商户已成为分店';
                    exit(json_encode($result));
                }
                if($mem_info[0]['branch_store_id'] == $uid){
                    $result['status'] = 'n';
                    $result['info'] = '您已是该商户的分店';
                    exit(json_encode($result));
                }
                if(empty($mem_info)){
                    $result['status'] = 'n';
                    $result['info'] = '未查询到总店信息';
                    exit(json_encode($result));
                }
                if($mem_info[0]['id'] == $uid){
                    $result['status'] = 'n';
                    $result['info'] = '不能绑定自己为总店';
                    exit(json_encode($result));
                }
                if($mem_info[0]['is_vip'] != 3){
                    $result['status'] = 'n';
                    $result['info'] = '该用户角色非商户';
                    exit(json_encode($result));
                }
                if($top_mem[0]['is_vip'] != 3){
                    $result['status'] = 'n';
                    $result['info'] = '当前用户角色非商户角色';
                    exit(json_encode($result));
                }
                // if(!empty($mem_info[0]['branch_store_id']) && $mem_info[0]['branch_store_id'] != $uid){
                //     $result['status'] = 'n';
                //     $result['info'] = '该店铺已绑定其他商户';
                //     exit(json_encode($result));
                // }
                // if(!empty($mem_info[0]['branch_store_id']) && $mem_info[0]['branch_store_id'] == $uid){
                //     $result['status'] = 'n';
                //     $result['info'] = '改店铺已绑定当前商户分店';
                //     exit(json_encode($result));
                // }
                $add_store= $mysql->query("UPDATE dyspw_member SET branch_store_id = {$mem_info[0]['id']} where id = {$uid}");
                if($add_store){
                    $result['status'] = 'y';
                    $result['identity'] = 'store';
                    $result['info'] = '该店铺成功绑定总店';
                    exit(json_encode($result));
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '总店绑定失败，请重新提交';
                    exit(json_encode($result));
                }
            }

        }

    }
 ?>
